﻿using Ledict.Core.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ledict.ConditionExtract.DAL
{
    public class CaseCauseCondDAL
    {
        private static string DbName = "samejudgelib";
        //private static string DbName2 = "JudgeDoc";

        /// <summary>
        /// 获取该节点下单一节点的数据集合
        /// </summary>
        /// <param name="ccId"></param>
        /// <returns></returns>
        public static DataTable GetExpress(string ccId)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string strSql = string.Format(@"select CaseId, CauseId, CC_TextCRCCode, CC_Formatting,'' as CC_GlobalOrder,'' as AllParentId  from SYS_Case_Cause_Cond
                                            where CHARINDEX(',{0},',','+AllParentId+',')>0
                                            order by CC_TextCRCCode,CauseId", ccId);
            DbCommand oCmd = db.GetSqlStringCommand(strSql.ToString());
            DataSet table = db.ExecuteDataSet(oCmd);
            if (table != null && table.Tables[0] != null && table.Tables[0].Rows.Count > 0)
            {
                return table.Tables[0];
            }
            else
            {
                return new DataTable();
            }                   
        }

        public static int GetExpressCount(string ccId)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string strSql = string.Format(@"select 1  from SYS_Case_Cause_Cond
                                            where AllParentId like '%,{0},%'
                                            order by CC_TextCRCCode", ccId);
            DbCommand oCmd = db.GetSqlStringCommand(strSql.ToString());
            DataSet table = db.ExecuteDataSet(oCmd);
            if (table != null && table.Tables[0] != null && table.Tables[0].Rows.Count > 0)
            {
                return table.Tables[0].Rows.Count;
            }
            else
            {
                return 0;
            }
        }


        /// <summary>
        /// 获取该节点下组合节点的数据集合
        /// </summary>
        /// <param name="ccId"></param>
        /// <returns></returns>
        public static DataTable getGroupNodes(string ccId)
        {
            Database db = DatabaseFactory.CreateDatabase(DbName);
            string strSql = string.Format(@"select CaseId,CauseId,a.CC_NodeType,CC_IsMatch,CC_AffirmIdeaResult,CC_Formatting,s.AllParentId+',' as AllParentId,CC_TextCRCCode, '' as CC_GlobalOrder  from SYS_Case_Cause_Cond s
											left join Ldt_JudgeDocSystem.dbo.SYS_CaseCause a on CauseId=a.CC_ID
                                             where CauseId != '{0}'
                                            and s.AllParentId like '%,{0},%'
                                            and CC_TextCRCCode in(select crcCode from fun_getExpressCrcCodeList('{0}',1))
                                            order by CC_TextCRCCode", ccId);
            DbCommand oCmd = db.GetSqlStringCommand(strSql.ToString());
            DataSet table = db.ExecuteDataSet(oCmd);
            if (table != null && table.Tables[0] != null && table.Tables[0].Rows.Count > 0)
            {
                return table.Tables[0];
            }
            else
            {
                return new DataTable();
            }
        }

    }
}
